package org.dioib.practice.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Builder;
import lombok.Data;
import org.springframework.http.HttpStatus;

import java.time.LocalDateTime;
import java.util.Map;

@Data
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ApiResponse<T> {
    private LocalDateTime timestamp;
    private int status;
    private String code;
    private String message;
    private String path;
    private T data;
    private Map<String, Object> details;


    public static <T>  ApiResponse<T> success(T data) {
        return ApiResponse.<T>builder()
                .status(HttpStatus.OK.value())
                .code("SUCCESS")
                .message("操作成功")
                .data(data)
                .timestamp(LocalDateTime.now())
                .build();
    }


    public static ApiResponse<?> error(int status, String code, String message, Map<String, Object> details) {
        return ApiResponse.builder()
                .status(status)
                .code(code)
                .message(message)
                .details(details)
                .timestamp(LocalDateTime.now())
                .build();
    }


    public static ApiResponse<?> error(int status, String code, String message) {
        return error(status, code, message, null);
    }
}